package com.fido.kingkongview.utils

import android.content.res.Resources
import android.graphics.Rect
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.fido.kingkongview.KingKongView
import com.fido.kingkongview.holder.KHolder
import com.fido.kingkongview.holder.KHolderCreator
import com.fido.kingkongview.indicator.KingKongIndicator


/**
 * @author: FiDo
 * @date: 2024/7/29
 * @des:
 */


internal val Number.dp
    get() = (this.toFloat() * Resources.getSystem().displayMetrics.density).toInt()

internal val Number.sp
    get() = (this.toFloat() * Resources.getSystem().displayMetrics.scaledDensity).toInt()


// 扩展函数，用于自定义滚动速度
internal fun RecyclerView.smoothScrollToPositionWithCustomSpeed(position: Int) {
    val linearSmoothScroller = object : LinearSmoothScroller(this.context) {
        override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
            return 150f / displayMetrics.densityDpi // 调整滚动速度
        }
    }
    linearSmoothScroller.targetPosition = position
    layoutManager?.startSmoothScroll(linearSmoothScroller)
}

internal fun Float.format(digits: Int) = "%.${digits}f".format(this)

internal fun <T> KingKongView<T>.findKingKongIndicator(): KingKongIndicator? {
    var v: KingKongIndicator? = null
    for (i in 0..this.childCount) {
        if (this.getChildAt(i) is KingKongIndicator) {
            v = this.getChildAt(i) as KingKongIndicator
            break
        }
    }
    return v
}

internal val RecyclerView.scrollPercent: Float
    get() {
        val scrollX = computeHorizontalScrollOffset()
        val maxScrollX = computeHorizontalScrollRange() - width
        return scrollX.toFloat() / maxScrollX.toFloat()
    }

internal fun View.height(height: Int): View {
    val params = layoutParams ?: ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
    params.height = height
    layoutParams = params
    return this
}

fun View.width(width: Int): View {
    val params = layoutParams ?: ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT)
    params.width = width
    layoutParams = params
    return this
}

internal fun View.margin(
    leftMargin: Int = Int.MAX_VALUE,
    topMargin: Int = Int.MAX_VALUE,
    rightMargin: Int = Int.MAX_VALUE,
    bottomMargin: Int = Int.MAX_VALUE
): View {
    val params = layoutParams as ViewGroup.MarginLayoutParams
    if (leftMargin != Int.MAX_VALUE)
        params.leftMargin = leftMargin
    if (topMargin != Int.MAX_VALUE)
        params.topMargin = topMargin
    if (rightMargin != Int.MAX_VALUE)
        params.rightMargin = rightMargin
    if (bottomMargin != Int.MAX_VALUE)
        params.bottomMargin = bottomMargin
    layoutParams = params
    return this
}

fun <T> easyKHolderCreator(
    @LayoutRes layoutRes: Int,
    onBind: (itemView: View,holder: KHolder<T>, data: T?, position: Int) -> Unit,
): KHolderCreator<T> {
    return object : KHolderCreator<T> {
        override fun creatorHolder(itemView: View): KHolder<T> =
            EasyHolder(itemView, onBind)

        override fun getLayoutId(): Int = layoutRes
    }
}

internal class EasyHolder<T>(
    itemView: View,
    private val onBind: (itemView:View,holder: KHolder<T>, data: T?, position: Int) -> Unit
) : KHolder<T>(itemView) {
    override fun initView(itemView: View) {}

    override fun onBind(holder: KHolder<T>, data: T?, position: Int) {
        this.onBind.invoke(itemView,holder, data, position)
    }
}

// item margin Decoration
internal class MarginItemDecoration(val span:Int,private val margin: Int) : ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val column = position % span
        //最左边
        outRect.left = if (column == 0) 0 else margin / 2
        //最右边
        outRect.right = if (column == (span -1)) 0 else margin / 2

        // Top and bottom margin can be added if needed
//        outRect.top = margin / 2
//        outRect.bottom = margin / 2
    }
}

